<?php
$__tpl['header'] = '';
$__tpl['footer'] = '';

$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
$catid = empty($_GET['catid']) ? 0 : intval($_GET['catid']);
$categorys = cache_read('goods_categorys.php');
$category_info = $categorys['category_list'][$catid];
if(empty($category_info['spec'])){
	exit('');
}
	$prototypes = Prototype::get_prototypes();
	if($id > 0){
		$oGoods = new Goods();
		$specs = $oGoods->get_specs($id, true);
		$specs = $specs['specs'];
	}
	$vars['specs'] = array();
	foreach($category_info['spec'] as $prototypeid => $spec_info){
		$prototype_info = $prototypes[$prototypeid];
		$spec_info['name'] = $prototype_info['name'];
		if(!empty($spec_info['regexp']) && empty($spec_info['prompt']))$spec_info['prompt'] = '...';
		if(is_array($prototype_info['value'])){
			$inputs = array();
			foreach($prototype_info['value'] as $key => $value){
				if(empty($spec_info['item'])){
					continue;
				}
				if(!in_array($key, $spec_info['item'])){
					continue;
				}
				if($id > 0 && isset($specs)){
					$checked = '';
					$price = '';
					$quantity = '';
					if(isset($specs[$prototypeid])){
						if(isset($specs[$prototypeid]['value'][$key])){
							$checked = ' checked';
							$price = $specs[$prototypeid]['value'][$key]['price'];
							if(empty($specs[$prototypeid]['value'][$key]['autofill']) 
								&& isset($specs[$prototypeid]['value'][$key]['quantity'])
								&& $specs[$prototypeid]['value'][$key]['quantity'] !== ''){
								$quantity = $specs[$prototypeid]['value'][$key]['quantity'];
							}
						}
					}
				}else{
					$checked = ' checked';	// 默认，分类指定的规格都使用
					$price = '';
					$quantity = '';
				}
				$inputs[] = '<label><input type="checkbox" name="specids['.$prototypeid.'][]" value="'.$key.'"'.$checked.' class="chbox" />'.$value.'</label>&nbsp;<input type="text" name="specs['.$prototypeid.']['.$key.']" value="'.$price.'" title="价格，留空则为统一值" class="input poshytip" style="width:50px;" />&nbsp;<input type="text" name="quantitys['.$prototypeid.']['.$key.']" value="'.$quantity.'" title="库存，留空则为统一值" class="input poshytip" style="width:50px;" />';
			}
			$spec_info['inputs'] = $inputs;
		}else{
			$checked_empty = '';
			if($id > 0 && isset($specs)){
				$checked = '';
				$price = '';
				$quantity = '';
				$default = '';
				if(isset($specs[$prototypeid])){
					$checked = ' checked';
					$price = $specs[$prototypeid]['price'];
					$default = $specs[$prototypeid]['value'];
					if(isset($specs[$prototypeid]['empty']) && $specs[$prototypeid]['empty'] == '1'){
						$checked_empty = ' checked';
					}
					if(empty($specs[$prototypeid]['autofill'])
							&& isset($specs[$prototypeid]['quantity']) 
							&& $specs[$prototypeid]['quantity'] !== ''){
						$quantity = $specs[$prototypeid]['quantity'];
					}
				}
			}else{
				$checked = ' checked';
				$price = '';
				$quantity = '';
				$default = trim($spec_info['default']);
			}
			$spec_info['inputs'] = array('<input type="checkbox" name="specids['.$prototypeid.']" value="'.$prototypeid.'"'.$checked.' />&nbsp;<input type="text" name="specs['.$prototypeid.'][value]" id="spec_'.$prototypeid.'" value="'.$default.'" class="input" style="width:160px;" />');//.'&nbsp;<input type="text" name="specs['.$prototypeid.'][price]" value="'.$price.'" style="width:50px;" />&nbsp;<input type="text" name="quantitys['.$prototypeid.'][quantity]" value="'.$quantity.'" style="width:50px;" />';
		}
		$vars['specs'][] = $spec_info;
	}
	

?>